package Aleehot100;

import java.util.Arrays;

/**
 * ClassName: c08
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/22 17:52
 * @Version 1.0
 * @Since 1.0
 */
public class c08 {
    public static void main(String[] args) {
        String str = "abcabcbb";
        System.out.println(lengthOfLongestSubstring(str));
    }


    public static int lengthOfLongestSubstring(String s) {
        char[] arr = s.toCharArray();
        int[] indexArr = new int[256];
        int r = 0, l = 0, res = 0;
        Arrays.fill(indexArr, -1);
        for (r = 0; r < arr.length; r++) {
            if(indexArr[arr[r]] == -1 || indexArr[arr[r]] < l) {
                res = Math.max(res, r - l + 1);
                indexArr[arr[r]] = r;
            } else {
                res = Math.max(res, r - l);
                l = indexArr[arr[r]] + 1;
                indexArr[arr[r]] = r;
            }
        }
        return res;
    }
}
